package org.dizem.intellipainter.component.stroke;

import org.dizem.intellipainter.model.StrokeType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * User: dizem@126.com
 * Time: 12-2-19 下午12:03
 */
public class StrokeChooser extends JPanel {
	private static final Font FONT = new Font("微软雅黑", Font.PLAIN, 13);
	private static final Dimension SIZE = new Dimension(70, 100);
	private final StrokeSelectCallback callback;
	private final java.util.List<StrokeButton> strokeButtonList = new ArrayList<StrokeButton>();

	public StrokeChooser(StrokeSelectCallback callback) {
		this.callback = callback;
		setLayout(new BorderLayout());
		setPreferredSize(SIZE);
		setMaximumSize(SIZE);
		setMinimumSize(SIZE);
		add(createCenter(), BorderLayout.CENTER);
		add(createSouth(), BorderLayout.SOUTH);
	}


	private Component createCenter() {
		JPanel center = new JPanel(new FlowLayout());

		JPanel panel = new JPanel(new GridLayout(4, 1, 3, 3));
		for (StrokeType type : StrokeType.values()) {
			final StrokeButton button = new StrokeButton(type, callback);
			strokeButtonList.add(button);
			panel.add(button);


			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					button.setSelected(true);
					for (StrokeButton other : strokeButtonList) {
						if (other != button) {
							other.setSelected(false);
						}
					}
				}
			});
		}
		center.add(panel);
		return center;
	}

	private JLabel createSouth() {
		JLabel label = new JLabel("粗细", JLabel.CENTER);
		label.setFont(FONT);
		label.setForeground(Color.GRAY);
		label.setPreferredSize(new Dimension(200, 20));
		return label;
	}

	public void setStrokeType(StrokeType type) {
		for (StrokeButton button : strokeButtonList) {
			if (button.getStrokeType() == type) {
				button.doClick();
				break;
			}
		}
	}
}
